package com.itaem.community.modules.infrastructure.user.mapper;

import com.itaem.community.modules.infrastructure.user.dto.UserDTO;
import com.itaem.community.modules.infrastructure.user.dto.UserPhoneCreateDTO;
import com.itaem.community.modules.infrastructure.user.dto.UserUpdateDTO;
import com.itaem.community.modules.infrastructure.user.dto.UserWechatCreateDTO;
import com.itaem.community.modules.infrastructure.user.entity.UserEntity;
import com.itaem.community.modules.infrastructure.user.entity.UserPhoneEntity;
import com.itaem.community.modules.infrastructure.user.entity.UserWechatEntity;
import com.itaem.community.modules.infrastructure.user.enums.Gender;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.springframework.stereotype.Component;

/**
 * <p>项目名称: community </p>
 * <p>文件名称: UserTransformer </p>
 * <p>描述: [类型描述] </p>
 * <p>创建时间: 2020/10/28 </p>
 * <p>作者信息: tomato</p>
 *
 * @author tomato
 * @version v1.0
 */
@Mapper(componentModel = "spring")
public interface UserTransformer {

    /**
     * 提取更新信息到实体类中
     *
     * @param updateDto 更新信息
     * @return 实体类
     */
    @Mapping(target = "token", ignore = true)
    @Mapping(target = "roleId", ignore = true)
    @Mapping(source = "gender", target = "gender", ignore = true, resultType = Gender.class)
    @Mapping(target = "deleted", expression = "java(Boolean.FALSE)")
    @Mapping(source = "updateDto.id", target = "id")
    UserEntity updateDtoToEntity(UserUpdateDTO updateDto);

    /**
     * 提取更新信息到实体类中（更新电话号码和密码）
     *
     * @param updateDto 更新信息
     * @return 实体类
     */
    @Mapping(target = "password", ignore = true)
    @Mapping(target = "salt", ignore = true)
    @Mapping(source = "updateDto.id", target = "userId")
    UserPhoneEntity phoneEntityFromUpdateDto(UserUpdateDTO updateDto);

    /**
     * 将数据传输实体转成数据库实体
     *
     * @param dto 数据传输实体
     * @return 数据库实体
     */
    @Mapping(target = "deleted", ignore = true)
    @Mapping(source = "dto.id", target = "id")
    UserEntity dtoToEntity(UserDTO dto);

    /**
     * 从用户实体+用户电话信息实体中提取信息组装用户信息
     *
     * @param userEntity  用户实体信息
     * @param phoneEntity 用户电话信息
     * @return 完整的用户信息
     */
    @Mapping(source = "userEntity.id", target = "id")
    UserDTO entityToDto(UserEntity userEntity, UserPhoneEntity phoneEntity);

    /**
     * 提取创建信息到用户电话实体中
     *
     * @param dto 创建信息
     * @return 用户电话实体
     */
    @Mapping(target = "id", ignore = true)
    @Mapping(target = "salt", ignore = true)
    @Mapping(target = "userId", ignore = true)
    UserPhoneEntity phoneDtoToPhoneEntity(UserPhoneCreateDTO dto);

    /**
     * 提取创建信息到用户微信实体中
     *
     * @param dto 创建信息
     * @return 用户微信创建实体
     */
    @Mapping(target = "id", ignore = true)
    @Mapping(target = "userId", ignore = true)
    UserWechatEntity wechatDtoToWechatEntity(UserWechatCreateDTO dto);
}
